package org.chnxi.study.messagequeue.rockermq.producer;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 发送同步消息，发送线程需要等待MQ的发送结果
 */
public class SyncProducer {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //使用默认提供的Producer
        DefaultMQProducer producer = new DefaultMQProducer();
        //设置NameServer地址
        producer.setNamesrvAddr("192.168.50.227:9876;192.168.50.227:9877");
        //启动Producer
        producer.start();

        //循环10次模拟消息发送
        for (int i=0; i<10; i++){
            //组装消息对象，设置topic、tag、msgCont，并获取发送结果
            Message msg = new Message("topicName" , "Tag" , (i+"_MessageContent").getBytes());
            SendResult result = producer.send(msg);

            SendStatus status = result.getSendStatus(); //获取发送结果
            result.getMsgId(); //Msg的ID
            result.getMessageQueue().getQueueId();//queueId
        }

        producer.shutdown();
    }

}
